﻿using System;

using System.Web.Mvc;

using PagedList;
using SIG.Service;
using SIG.Model.ViewModel.Back;
using SIG.Basic.Helper;
using SIG.Model;

namespace SIG.WebUI.Areas.Admin.Controllers
{
    [Authorize]
    //[InitializeSimpleMembership]
    public class CarouselController : Controller
    {
        //private ApplicationUnit _unit = new ApplicationUnit();

        private readonly ICarouselService _carouselService;
        public CarouselController(ICarouselService carouselService)
        {
            _carouselService = carouselService;
        }

        //
        // GET: /Admin/Carousel/

         [Authorize(Roles = "创始人,管理员")]
        public ActionResult Index(int? page)
        {
          
            var carouselList = new CarouselListVM
            {
                PageIndex = (page ?? 1) - 1,
                PageSize = Site.Settings.Carousel.PageSize,
                TotalCount = _carouselService.CarouselsCount()
            };
             carouselList.Carousels = _carouselService.GetPageingCarousels(carouselList.PageIndex, carouselList.PageSize);

             var carouselsAsIPagedList = new StaticPagedList<Carousel>(carouselList.Carousels,
                 carouselList.PageIndex + 1, carouselList.PageSize, carouselList.TotalCount);
             ViewBag.OnePageOfCarousels = carouselsAsIPagedList;

            return View(carouselList);
        }
         [Authorize(Roles = "创始人,管理员")]

        public ActionResult AddCarousel()
        {
            Carousel vCarousel = new Carousel();
            vCarousel.Active = true;
            vCarousel.Importance = 0;

            return View("AddCarousel", vCarousel);
        }


        [HttpPost]
         [Authorize(Roles = "创始人,管理员")]
        public JsonResult AddCarousel(Carousel carousel)
        {



            if (ModelState.IsValid)
            {
                _carouselService.Create(carousel);
             //   _unit.SaveChanges();

                // TempData["message"] = string.Format("{0} 已经保存", carousel.Title);
                return Json(true, JsonRequestBehavior.DenyGet);

            }
            else
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }
            //   return RedirectToAction("Index");

        }

         [Authorize(Roles = "创始人,管理员")]
        public ActionResult EditCarousel(int Id)
        {
            var vCarousel = _carouselService.GetById(Id);
            if (vCarousel != null)
            {
                return View(vCarousel);
            }
            return View("NotFound");
        }

        [HttpPost]
         [Authorize(Roles = "创始人,管理员")]
        public JsonResult EditCarousel(Carousel Carousel)
        {


            //carousel.UpdatedBy = User.Identity.Name;
            //carousel.UpdatedDate = DateTime.Now;   

            if (ModelState.IsValid)
            {
                _carouselService.Update(Carousel);
               // _unit.SaveChanges();
                return Json(true, JsonRequestBehavior.DenyGet);

            }
            else
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }

        }

        // DELETE: /User/DeleteCarousel
        [HttpDelete]
         [Authorize(Roles = "创始人,管理员")]
        public JsonResult DeleteCarousel(int id)
        {

            var vCarousel = _carouselService.GetById(id);

            if (vCarousel != null)
            {
                string imgurl = Server.MapPath(vCarousel.ImageUrl);
                if (System.IO.File.Exists(imgurl))
                {
                    System.IO.File.Delete(imgurl);    //删除缩略图
                }

                _carouselService.Delete(vCarousel);
             //   _unit.SaveChanges();
                return Json(true, JsonRequestBehavior.DenyGet);
            }

            return Json(false, JsonRequestBehavior.DenyGet);
        }



        [HttpPut]
        public JsonResult ActiveCarousel(int id)
        {

            var vCarousel = _carouselService.GetById(id);
            try
            {
                if (vCarousel != null)
                {
                    vCarousel.Active = !vCarousel.Active;
                    _carouselService.Update(vCarousel);
                    //_unit.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }

            return Json(true, JsonRequestBehavior.DenyGet);


        }

    }
}
